package com.example.yichuangyun.mvvm.ui.fragment.view;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.model.company.AreaModel;
import com.example.yichuangyun.model.company.CompanyDataModel;
import com.example.yichuangyun.model.company.CompanyDetailsContactsDataModel;
import com.example.yichuangyun.model.company.CompanyListParamsModel;
import com.example.yichuangyun.mvvm.base.BaseViewModel;
import com.example.yichuangyun.mvvm.di.retrofit.BaseModel;
import com.example.yichuangyun.mvvm.di.retrofit.BaseNetCallback;
import com.example.yichuangyun.utils.ToastHelper;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 易企搜
 */
public class SearchCompanyViewModel extends BaseViewModel {

    public MutableLiveData<List<AreaModel>> areaList = new MutableLiveData<>();
    public MutableLiveData<CompanyDataModel> companyDataModelMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<String> getListErrorMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<String> collectSuccessMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<String> cancelCollectSuccessMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<Integer> position = new MutableLiveData<>();
    public MutableLiveData<List<CompanyDetailsContactsDataModel.ContactsModel>> contactsMutableLiveData = new MutableLiveData<>();

    /**
     * 获取所有区域
     */
    public void getAreaList() {
        addSubscribe(mRetrofitHelper.getAreaList().subscribeWith(new BaseNetCallback<List<AreaModel>>() {

            @Override
            public void onSuccess(BaseModel<List<AreaModel>> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (null != baseModel.getData()) {
                        areaList.setValue(baseModel.getData());
                    }
                }
            }
        }));
    }

    /**
     * 获取企业列表
     */
    public void getCompanyList(CompanyListParamsModel paramsModel) {
        Log.e("@@@@@@@@###", new Gson().toJson(paramsModel));
        addSubscribe(mRetrofitHelper.getCompanyList(new Gson().toJson(paramsModel)).subscribeWith(new BaseNetCallback<CompanyDataModel>() {

            @Override
            public void onSuccess(BaseModel<CompanyDataModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (null != baseModel.getData()) {
                        companyDataModelMutableLiveData.setValue(baseModel.getData());
                    }
                } else {
                    getListErrorMutableLiveData.setValue("error");
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                getListErrorMutableLiveData.setValue("error");
            }
        }));
    }

    public void getCompanyList(Map<String, Object> map) {
        map.put("page", 1);
        addSubscribe(mRetrofitHelper.getCompanyList(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<CompanyDataModel>() {

            @Override
            public void onSuccess(BaseModel<CompanyDataModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (null != baseModel.getData()) {
                        companyDataModelMutableLiveData.setValue(baseModel.getData());
                    }
                } else {
                    getListErrorMutableLiveData.setValue("error");
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                getListErrorMutableLiveData.setValue("error");
            }
        }));
    }

    /**
     * 收藏企业
     */
    public void collectCompany(CompanyDataModel.CompanyModel companyModel) {
        JsonArray jsonArray = new JsonArray();
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("companyName", companyModel.getName());
        jsonObject.addProperty("creditNo", companyModel.getCreditNo());
        jsonObject.addProperty("eid", companyModel.getEid());
        jsonArray.add(jsonObject);
        Map<String, Object> map = new HashMap<>();
        map.put("collectSaves", jsonArray);
        addSubscribe(mRetrofitHelper.collectCompany(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<String>() {

            @Override
            public void onSuccess(BaseModel<String> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    collectSuccessMutableLiveData.setValue(baseModel.getData());
                } else {
                    ToastHelper.show(baseModel.getMsg());
                }
            }
        }));
    }

    /**
     * 取消收藏企业
     */
    public void cancelCollectCompany(String eid) {
        addSubscribe(mRetrofitHelper.cancelCollectCompany(eid).subscribeWith(new BaseNetCallback<String>() {

            @Override
            public void onSuccess(BaseModel<String> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    cancelCollectSuccessMutableLiveData.setValue(baseModel.getData());
                } else {
                    ToastHelper.show(baseModel.getMsg());
                }
            }
        }));
    }

    /**
     * 获取可拨打电话
     */
    public void getContacts(String eid) {
        addSubscribe(mRetrofitHelper.getContacts(eid).subscribeWith(new BaseNetCallback<List<CompanyDetailsContactsDataModel.ContactsModel>>() {

            @Override
            public void onSuccess(BaseModel<List<CompanyDetailsContactsDataModel.ContactsModel>> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    contactsMutableLiveData.setValue(baseModel.getData());
                }
            }
        }));
    }
}
